package hrm;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    //进入登录界面的url
    @RequestMapping(value = "/toLogin")
    public String toLoginForm(){
        return "loginForm";
    }

    //处理登录的url
    @ResponseBody
    @RequestMapping(value = "/login")
    public String login(@RequestParam("loginId") String loginId,
                        @RequestParam("password") String password,
                        HttpSession session){

        try{
            Thread.sleep(1000);//模拟网络延迟
        }catch (InterruptedException e){

        }
        logger.info("进到了login方法");

        if("".equals(loginId) || "".equals(password)) {
            logger.info("账号或密码为空，不访问数据库，直接返回null");
            return null;
        }
        if("123".equals(loginId) || "123".equals(password)){
            logger.info("登录成功，准备跳往主界面");
            return "/user/main";
        }else{
            logger.info("登录失败，账号密码错误");
            return null;
        }
    }

    //登录验证通过后跳转的url
    @RequestMapping(value = "/main")
    public ModelAndView toMainWindow(ModelAndView mv){
        logger.info("准备进行页面跳转");
        mv.setViewName("main");
        return mv;
    }

}
